package com.huanying.http;

import java.math.BigDecimal;
import java.sql.Timestamp;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;

import com.huanying.framework.BaseDao;

public class GetGj implements GetWeb {
	@Autowired
	private BaseDao baseDao;
	
	Logger logger = Logger.getLogger(GetGj.class);

	@Override
	public void get() throws Exception {
		Document doc;
		Element ul;
		Elements all_buliding;
		Element link;
		String linkUrl;
		Elements li_list;
		
		String name;	//房屋名称
		String city;	//城市
		String county;	//区县
		String community;	//小区
		String shape;	//户型
		String floor;		//楼层
		String build_space;		//建筑面积
		String inside_space;	//套内面积
		BigDecimal total_price;	//总价
		BigDecimal unit_price;	//单价
		String direction;	//朝向
		String build_time;	//建筑年代
		Timestamp creat_time;	//发布时间
		Timestamp refresh_time;	//刷新时间
		int fitment_type;	//装修标准	
		int build_type;		//房屋类型	1多层住宅  2别墅  3公寓  4
		int sale_status;	//销售状态	1开发商留房  2可售  3小订  4大订  5首付交清  6银行放款  6销售完成
		String remark;		//备注
		
		
		for (int j = 1; j <= 1; j++) {
			doc = Jsoup.connect("http://langfang.ganji.com/fang5/guangyang/a1o/" + j + "/").get();
			ul = doc.getElementsByAttributeValue("class", "list-style1").get(1);
			all_buliding = ul.getElementsByTag("li");
			
			for (int i = 0; i < all_buliding.size(); i++) {
				
				link = all_buliding.get(i).getElementsByAttributeValue("class","list-mod1").first().getElementsByTag("a").first();
				linkUrl = link.attr("href");
				doc = getBuilding("http://langfang.ganji.com"+linkUrl);
				
				name = doc.getElementsByTag("h1").text();
				li_list = doc.getElementsByAttributeValue("class", "basic-info").first().getElementsByTag("ul").first().getElementsByTag("li");
				
				total_price = new BigDecimal(li_list.get(0).getElementsByAttributeValue("class", "basic-info-price").first().html().trim());
				String price = li_list.get(1).text();
				price = price.substring(2, price.indexOf("元"));
				Sale b = new Sale();
				b.setName(name);
				
				baseDao.save(b);
			}
		}
	}
	
	@Override
	public Document getBuilding(String url) throws Exception {
		Document doc;
		try {
			if(url!=null || !("").equals(url)){
				doc = Jsoup.connect(url).get();
				return doc;
			}
			else{
				return null;
			}
		} catch (Exception e) {
			return null;
		}
	}

	public String getPhone(Document doc, int k) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public String getPrice(Document doc) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public String getShape(Document doc) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
